﻿using UnityEngine;
using System.Collections.Generic;
using SkillSystem;
using BehaviorSystem;

public class SkillTransmitNode : SequenceNode
{
    SkillTransmitComponnent skillTransmitComponnent;
    List<SkillItemData> skillItemDatas = new List<SkillItemData>();
    public SkillTransmitNode(IBehaviorAgent agent) : base(agent)
    {
        skillTransmitComponnent = agent.GetComponent<SkillTransmitComponnent>();
        for (int i = 0; i < skillTransmitComponnent.CurSkillData.SkillItems.Count; i++)
        {
            SkillItemData itemData=SkillSystemFacade.Inst.GetSkillItemData(skillTransmitComponnent.CurSkillData.SkillItems[i]);
            if (itemData != null) 
            {
                skillItemDatas.Add(itemData);
            }
        }
        SerializeSkillExcutor();
        CurState = BTNodeState.Invalid;
    }

    void SerializeSkillExcutor()
    {
        for (int i = 0; i < skillItemDatas.Count; i++)
        {
            this.ChildrenNodes.Add(new SkillExcuteNode(this.BehaviourAgent, skillItemDatas[i]));
            if (skillItemDatas[i].WaitTime!=0)
            {
                this.ChildrenNodes.Add(new WaitNode(BehaviourAgent, skillItemDatas[i].WaitTime));
                this.ChildrenNodes.Add(new SkillItemCloseNode(this.BehaviourAgent, skillItemDatas[i]));
            }
            
        }
    }

    public override void OnExcuted()
    {
        if (skillTransmitComponnent.IsActive)
        {
            CurState = BTNodeState.Running;
        }
        else
        {
            CurState = BTNodeState.Invalid;
        }
    }

    public override void OnReseted()
    {
        if (CurState==BTNodeState.Success||CurState==BTNodeState.Failure)
        {
            skillTransmitComponnent.IsActive = false;
        }

        CurState = BTNodeState.Invalid;
    }
}
